package cn.cxion.chat.app.activity;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ToastUtils;
import com.google.android.material.appbar.AppBarLayout;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.cxion.chat.R;
import cn.cxion.chat.app.constant.Constant;
import cn.cxion.chat.app.util.LogUtil;
import cn.cxion.chat.app.util.StringUtil;
import cn.cxion.chat.kit.WfcBaseActivity;

public class RegisterActivity extends WfcBaseActivity {
    @Bind(R.id.appbar)
    AppBarLayout appbar;
    @Bind(R.id.et_company)
    EditText etCompany;
    @Bind(R.id.et_name)
    EditText etName;
    @Bind(R.id.et_phone)
    EditText etPhone;
    @Bind(R.id.et_email)
    EditText etEmail;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.cb_agree)
    CheckBox cbAgree;
    @Bind(R.id.tv_legalagreement)
    TextView tvLegalagreement;
    @Bind(R.id.tv_privatepolicy)
    TextView tvPrivatepolicy;
    @Bind(R.id.tv_submitregister)
    TextView tvSubmitregister;

    @Override
    protected int contentLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        ButterKnife.bind(this);
        tvSubmitregister.setClickable(false);
        cbAgree.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                check();
            }
        });
    }

    @OnClick({R.id.tv_legalagreement, R.id.tv_privatepolicy, R.id.tv_submitregister})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_legalagreement:
                ConstantActivity.show(RegisterActivity.this, Constant.LEGALAGREEMENT);

                break;
            case R.id.tv_privatepolicy:
                ConstantActivity.show(RegisterActivity.this, Constant.PRIVATEPOLICY);

                break;
            case R.id.tv_submitregister:
                checkStep2();
                break;
        }
    }

    @OnTextChanged(value = R.id.et_company, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void etCompany(Editable editable) {
        check();
    }

    @OnTextChanged(value = R.id.et_name, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void etName(Editable editable) {
        check();
    }

    @OnTextChanged(value = R.id.et_phone, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void etPhone(Editable editable) {
        check();
    }

    @OnTextChanged(value = R.id.et_email, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void etEmail(Editable editable) {
        check();
    }

    @OnTextChanged(value =( R.id.et_password), callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void etPassword(Editable editable) {
        check();
    }

    private void check() {
        if (checkStringIsEmpty(etCompany, etName, etPhone, etEmail, etPassword)) {
            closeClick();
            return;
        }
        if (cbAgree.isChecked()) {
            openClick();
        } else {
            closeClick();
        }
    }
    private void checkStep2(){
        if(!StringUtil.istruePhoneNum(etPhone.getText().toString())){
            ToastUtils.showShort("请输入正确的手机号");
        }else if(!StringUtil.isEmail(etEmail.getText().toString())){
            ToastUtils.showShort("请输入正确的邮箱地址");
        }else if(etPassword.getText().toString().length()<6){
            ToastUtils.showShort("密码必须大于六位数");
        }else{
            finish();
        }
    }

    private boolean checkStringIsEmpty(EditText... editText) {
        boolean b = false;
        for (int i = 0; i < (editText == null ? 0 : editText.length); i++) {
            if (StringUtil.isEmpty(editText[i].getText().toString())) {
                b = true;
                break;
            } else {
                b = false;
            }
        }
        return b;
    }

    private void closeClick() {
        tvSubmitregister.setBackgroundResource(R.drawable.login_btn_gray_d);
        tvSubmitregister.setClickable(false);
    }

    private void openClick() {
        tvSubmitregister.setBackgroundResource(R.drawable.login_btn_red);
        tvSubmitregister.setClickable(true);
    }

}
